package com.jh.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jh.pojo.IPCamRadarInfo;
import com.jh.service.impl.IPCamRadarInfoSrvImpl;
import com.jh.utils.CacheUtils;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    static final Logger logger = LoggerFactory.getLogger(LoginServlet.class);

    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String userName = request.getParameter("userName");
        String userPwd = request.getParameter("userPwd");
        HttpSession s = request.getSession();
        String sessionId = s.getId();

        Object o1 = s.getServletContext().getAttribute("USER_PARAM1");
        Object o2 = s.getServletContext().getAttribute("USER_PARAM2");
        Object wsUrlObj = s.getServletContext().getAttribute("WSURL");
        CacheUtils cacheUtils = CacheUtils.getInstance();
        cacheUtils.listPrint();
        System.out.println("userName: [" + userName + "] - userPwd: [" + userPwd + "]" + "  USER_PARAM1: " + "["
                + o1.toString() + "]" + "  USER_PARAM2: " + "[" + o2.toString() + "]");
        System.out.println("sessionId :[" + sessionId + "]");
        logger.debug("---> userName: " + userName + "login successed! sessionId: " + sessionId);
        boolean rst = true;// 访问数据库
        if (rst) {
            IPCamRadarInfoSrvImpl srv = new IPCamRadarInfoSrvImpl();
            /*从mysql中获取设备列表*/
            List<IPCamRadarInfo> list = srv.list(null, true);
            s.setAttribute("loginName", userName);
            s.setAttribute("list", list);
            s.setAttribute("CACHE_HASHMAP", cacheUtils.getCache());
            s.setAttribute("WSURL", wsUrlObj.toString());

            request.getRequestDispatcher("/index.jsp").forward(request, response);
        } else {
            request.setAttribute("info", "账号输入错误！");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }

    }

}
